package cn.iocoder.yudao.module.member.controller.admin.scorelevel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.scorelevel.vo.*;
import cn.iocoder.yudao.module.member.convert.scorelevel.ScoreLevelConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scorelevel.ScoreLevelDO;
import cn.iocoder.yudao.module.member.service.scorelevel.ScoreLevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 积分等级")
@RestController
@RequestMapping("/member/score-level")
@Validated
public class ScoreLevelController {

    @Resource
    private ScoreLevelService scoreLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建积分等级")
    @PreAuthorize("@ss.hasPermission('member:score-level:create')")
    public CommonResult<Long> createScoreLevel(@Valid @RequestBody ScoreLevelCreateReqVO createReqVO) {
        return success(scoreLevelService.createScoreLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分等级")
    @PreAuthorize("@ss.hasPermission('member:score-level:update')")
    public CommonResult<Boolean> updateScoreLevel(@Valid @RequestBody ScoreLevelUpdateReqVO updateReqVO) {
        scoreLevelService.updateScoreLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:score-level:delete')")
    public CommonResult<Boolean> deleteScoreLevel(@RequestParam("id") Long id) {
        scoreLevelService.deleteScoreLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:score-level:query')")
    public CommonResult<ScoreLevelRespVO> getScoreLevel(@RequestParam("id") Long id) {
        ScoreLevelDO scoreLevel = scoreLevelService.getScoreLevel(id);
        return success(ScoreLevelConvert.INSTANCE.convert(scoreLevel));
    }

    @GetMapping("/list")
    @Operation(summary = "获得积分等级列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:score-level:query')")
    public CommonResult<List<ScoreLevelRespVO>> getScoreLevelList(@RequestParam("ids") Collection<Long> ids) {
        List<ScoreLevelDO> list = scoreLevelService.getScoreLevelList(ids);
        return success(ScoreLevelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分等级分页")
    @PreAuthorize("@ss.hasPermission('member:score-level:query')")
    public CommonResult<PageResult<ScoreLevelRespVO>> getScoreLevelPage(@Valid ScoreLevelPageReqVO pageVO) {
        PageResult<ScoreLevelDO> pageResult = scoreLevelService.getScoreLevelPage(pageVO);
        return success(ScoreLevelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分等级 Excel")
    @PreAuthorize("@ss.hasPermission('member:score-level:export')")
    @OperateLog(type = EXPORT)
    public void exportScoreLevelExcel(@Valid ScoreLevelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ScoreLevelDO> list = scoreLevelService.getScoreLevelList(exportReqVO);
        // 导出 Excel
        List<ScoreLevelExcelVO> datas = ScoreLevelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "积分等级.xls", "数据", ScoreLevelExcelVO.class, datas);
    }

}
